package com.sistemagestion.productos.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.sistemagestion.R;
import com.sistemagestion.BO.RubroBO;
import com.sistemagestion.Entities.Producto;
import com.sistemagestion.Entities.Rubro;

public class ProductoAdapter extends ArrayAdapter<Producto>{

	private List<Producto> _productos, allproductos;
	private int res;
	
	public ProductoAdapter(Context context, int textViewResourceId,
			List<Producto> objects) {
		super(context, textViewResourceId, objects);
		this.res = textViewResourceId;
		this._productos = objects;
		this.allproductos = objects;
	}

	@Override
	public void add(Producto object) {
		_productos.add(object);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return _productos.size();
	}

	@Override
	public Filter getFilter() {
		return new ProductoFilter();
	}

	@Override
	public Producto getItem(int position) {
		return _productos.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewholder;
		if (convertView == null){
			viewholder = new ViewHolder();
			LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(res, null);
			viewholder.txtCodigo = (TextView)convertView.findViewById(R.id.txtCodigo);
			viewholder.txtDescripcion = (TextView)convertView.findViewById(R.id.txtDescripcion);
			viewholder.txtPrecio = (TextView)convertView.findViewById(R.id.txtPrecio);
			viewholder.txtStock = (TextView)convertView.findViewById(R.id.txtStock);
			viewholder.color = (View)convertView.findViewById(R.id.color);
			convertView.setTag(viewholder);
		}
		else {
			viewholder = (ViewHolder)convertView.getTag();
		}
		
		Producto prod = (Producto)getItem(position);
		viewholder.txtCodigo.setText(Integer.toString(prod.getCodigo()));
		viewholder.txtDescripcion.setText(prod.getNombre());
		viewholder.txtPrecio.setText(Float.toString(prod.getPrecioVenta()));
		viewholder.txtStock.setText(Integer.toString(prod.getStock()));
		viewholder.color.setBackgroundResource(R.drawable.gradientgreen);
		
		return convertView;
	}

	@Override
	public void remove(Producto object) {
		allproductos.remove(object);
		_productos.remove(object);
		notifyDataSetChanged();
	}
	
	private static class ViewHolder{
		TextView txtDescripcion;
		TextView txtPrecio;
		TextView txtStock;
		TextView txtCodigo;
		View color;
	}
	
	public class ProductoFilter extends Filter{

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {


			FilterResults filterResults = new FilterResults();
			List<Producto> productosFiltrados = new ArrayList<Producto>();
			if (constraint != null && constraint.length()>0){
				constraint = constraint.toString().toLowerCase();
				for (Producto prod: allproductos){
					String text = prod.toString().toLowerCase();
					if (text.contains(constraint) && text.startsWith(constraint.toString())){
						productosFiltrados.add(prod);
					}
				}
			}
			else{
				productosFiltrados = allproductos;
			}
			filterResults.count = productosFiltrados.size();
			filterResults.values = productosFiltrados;
			return filterResults;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			_productos = (List<Producto>)results.values;
			notifyDataSetChanged();
			
		}
		
	}
	
}
